package com.ruoyi.web.controller.mtproject;
import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.domain.MtContact;
import com.ruoyi.service.IMtContactService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.MtPrice;
import com.ruoyi.service.IMtPriceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 维保费金额明细Controller
 * 
 * @author ruoyi
 * @date 2020-07-04
 */
@Controller
@RequestMapping("/ruoyi/price")
public class MtPriceController extends BaseController
{
    private String prefix = "ruoyi/price";

    @Autowired
    private IMtPriceService mtPriceService;
    @Autowired
    private IMtContactService mtContactService;

    @RequiresPermissions("ruoyi:price:view")
    @GetMapping("/price/{mtContractId}")
    public String price(@PathVariable("mtContractId") Long mtContractId,ModelMap mmap)
    {
        mmap.put("mtContractId",mtContractId);
        return prefix + "/price";
    }

    /**
     * 查询维保费金额明细列表
     */
    @RequiresPermissions("ruoyi:price:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MtPrice mtPrice)
    {
        startPage();
        List<MtPrice> list = mtPriceService.selectMtPriceList(mtPrice);
        return getDataTable(list);
    }

    /**
     * 查询维保费金额明细列表
     */
    @RequiresPermissions("ruoyi:price:list")
    @PostMapping("/lists/{mtContractId}")
    @ResponseBody
    public TableDataInfo lists(@PathVariable("mtContractId") Long mtContractId)
    {

        startPage();
        List<MtPrice> lists = mtPriceService.selectMtPriceListById(mtContractId);
        //循环取值
        for(int i =0;i<lists.size();i++){
            //如果未支付金额为0的时候则把支付状态改为已全部支付
            if(lists.get(i).getMtCostUnpaid().compareTo(BigDecimal.ZERO)==1){
                lists.get(i).setMtCostIspaid(1L);
                mtPriceService.updateMtPrice(lists.get(i));
                //否则支付状态为未完全支付
            }else{
                lists.get(i).setMtCostIspaid(0L);
                mtPriceService.updateMtPrice(lists.get(i));
            }
        }
        return getDataTable(lists);
    }

    /**
     * 导出维保费金额明细列表
     */
    @RequiresPermissions("ruoyi:price:export")
    @Log(title = "维保费金额明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export/{mtContractId}")
    @ResponseBody
    public AjaxResult export(@PathVariable("mtContractId")Long mtContractId,MtPrice mtPrice)
    {
        mtPrice.setMtContractId(mtContractId);
        List<MtPrice> list = mtPriceService.selectMtPriceList(mtPrice);
        ExcelUtil<MtPrice> util = new ExcelUtil<MtPrice>(MtPrice.class);
        return util.exportExcel(list, "维保费金额明细");
    }

    /**
     * 新增维保费金额明细
     */
    @GetMapping("/add/{mtContractId}")
    public String add(@PathVariable("mtContractId") Long mtContractId, ModelMap mmap)
    {
        mmap.put("mtContractId",mtContractId);
        return prefix + "/add";
    }

    /**[
     * 新增保存维保费金额明细
     */
    @RequiresPermissions("ruoyi:price:add")
    @Log(title = "维保费金额明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MtPrice mtPrice)
    {
        //添加时判断
        //未支付金额为null的时候未支付金额则等于总金额;
        if (mtPrice.getMtCostUnpaid()==null){
            mtPrice.setMtCostUnpaid(mtPrice.getMtCostPrice());
        }
        //已支付金额为null的时候已支付金额为0.00;
        if(mtPrice.getMtCostPaid()==null){
            BigDecimal bd =  BigDecimal.valueOf(0.00);
            mtPrice.setMtCostPaid(bd);
        }
        //将支付状态设置为0;
        mtPrice.setMtCostIspaid(0L);

        //判断本次输入的付款金额+已支付金额不能大于项目总金额

        MtContact mtContact = mtContactService.selectMtContactById(mtPrice.getMtContractId());
        // 创建一个变量初始值为提交的金额;
        BigDecimal s=mtPrice.getMtCostPrice();
        //判断如果提交的金额大于合同金额回滚
        if(mtPrice.getMtCostPrice().compareTo(mtContact.getMtPrice())==1){
            return toAjax(0);
        }
        List <MtPrice> listss = mtPriceService.selectMtPriceListById(mtPrice.getMtContractId());
        for(int j =0;j<listss.size();j++){
            //把本次提交的金额和数据库的所有金额相加
            s=s.add(listss.get(j).getMtCostPrice());
        }
        //如果大于合同金额则回滚
        if(s.compareTo(mtContact.getMtPrice())==1){
                return toAjax(0);
        }else{
            //否则添加进数据库
            return toAjax(mtPriceService.insertMtPrice(mtPrice));
        }
    }

    /**
     * 修改维保费金额明细
     */
    @GetMapping("/edit/{mtCostId}")
    public String edit(@PathVariable("mtCostId") Long mtCostId, ModelMap mmap)
    {
        MtPrice mtPrice = mtPriceService.selectMtPriceById(mtCostId);
        mmap.put("mtPrice", mtPrice);
        return prefix + "/edit";
    }

    /**
     * 修改保存维保费金额明细
     */
    @RequiresPermissions("ruoyi:price:edit")
    @Log(title = "维保费金额明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MtPrice mtPrice)
    {
        MtPrice mtPrice1 = mtPriceService.selectMtPriceById(mtPrice.getMtCostId());
        // 计算总已付余额(原先的已付金额+新加的已付余额)
        //如果已付金额等于null
        if(mtPrice1.getMtCostPaid()==null&&mtPrice.getMtCostPaid().compareTo(mtPrice1.getMtCostPrice())<1){
            mtPrice.getMtCostPaid();
            mtPrice.setMtCostUnpaid(mtPrice1.getMtCostPrice().subtract(mtPrice.getMtCostPaid()));
            //如果已付金额不等于null
        }else if(mtPrice1.getMtCostPaid()!=null){
            if(mtPrice1.getMtCostPaid().add(mtPrice.getMtCostPaid()).compareTo(mtPrice1.getMtCostPrice())==1){
                return toAjax(0);
            }else {
                BigDecimal sum=mtPrice1.getMtCostPaid().add(mtPrice.getMtCostPaid());
                mtPrice.setMtCostPaid(sum);
                //  计算未付金额(催款总金额-已付金额)
                if(mtPrice1.getMtCostUnpaid()!=null) {
                    BigDecimal unsum = mtPrice1.getMtCostPrice().subtract(sum);
                    mtPrice.setMtCostUnpaid(unsum);
                }else{
                    mtPrice.setMtCostUnpaid(mtPrice1.getMtCostPrice().subtract(mtPrice.getMtCostPaid()));
                }
            }
        }else{
            return toAjax(0);
        }
        return toAjax(mtPriceService.updateMtPrice(mtPrice));
    }

    /**
     * 删除维保费金额明细
     */
    @RequiresPermissions("ruoyi:price:remove")
    @Log(title = "维保费金额明细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mtPriceService.deleteMtPriceByIds(ids));
    }
}
